const fs = require('fs');
const path = require('path');

const copyDirectorySync = (src, dest) => {
    if (!fs.existsSync(src)) {
        return false;
    }
    if (!fs.existsSync(dest)) {
        fs.mkdirSync(dest, { recursive: true });
    }
    var dirs = fs.readdirSync(src);
    dirs.forEach(function (item) {
        var item_path = path.join(src, item);
        var temp = fs.statSync(item_path);
        if (temp.isFile()) {
            fs.copyFileSync(item_path, path.join(dest, item));
        } else if (temp.isDirectory()) {
            // console.log("Item Is Directory:" + item);
            copyDirectorySync(item_path, path.join(dest, item));
        }
    });
};

// 复制 build-web 和 build-server
fs.existsSync('./build-web') && fs.rmdirSync('./build-web', { recursive: true });
copyDirectorySync('../web/build-web', './build-web');

fs.existsSync('./build-server') && fs.rmdirSync('./build-server', { recursive: true });
copyDirectorySync('../server/build-server', './build-server');

// 复制 ffmpeg
const os = process.argv[2]; // package.json 中的 build 命令对应的参数：mac/windows
console.log('os: ' + os);

fs.existsSync('./static') && fs.rmdirSync('./static', { recursive: true });
copyDirectorySync('../server/static', './static');

switch (os) {
    case 'mac':
        // copyDirectorySync('../server/static', './static');
        // fs.chmodSync('./static/darwin-x64-apple/ffmpeg', 511);
        break;
    case 'win':
        // copyDirectorySync('../server/static/win-x64', './static/win-x64');
        // fs.chmodSync('./static/win-x64/ffmpeg.exe', 511);
        break;
}

console.log('----copy_files over----\n');
